<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveRecord::Locking::Optimistic</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" />
<script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActiveRecord::Locking::Optimistic 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../../files/activerecord/lib/active_record/locking/optimistic_rb.html">activerecord/lib/active_record/locking/optimistic.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<h2 id="label-What+is+Optimistic+Locking">What is <a href="Optimistic.html">Optimistic</a> <a href="../Locking.html">Locking</a></h2>

<p><a href="Optimistic.html">Optimistic</a> locking allows multiple users to
access the same record for edits, and assumes a minimum of conflicts with
the data. It does this by checking whether another process has made changes
to a record since it was opened, an
<code>ActiveRecord::StaleObjectError</code> exception is thrown if that has
occurred and the update is ignored.</p>

<p>Check out <code>ActiveRecord::Locking::Pessimistic</code> for an
alternative.</p>

<h2 id="label-Usage">Usage</h2>

<p>Active Records support optimistic locking if the field
<code>lock_version</code> is present. Each update to the record increments
the <code>lock_version</code> column and the locking facilities ensure that
records instantiated twice will let the last one saved raise a
<code>StaleObjectError</code> if the first was also updated. Example:</p>

<pre>p1 = Person.find(1)
p2 = Person.find(1)

p1.first_name = &quot;Michael&quot;
p1.save

p2.first_name = &quot;should fail&quot;
p2.save # Raises a ActiveRecord::StaleObjectError</pre>

<p><a href="Optimistic.html">Optimistic</a> locking will also check for stale
data when objects are destroyed. Example:</p>

<pre>p1 = Person.find(1)
p2 = Person.find(1)

p1.first_name = &quot;Michael&quot;
p1.save

p2.destroy # Raises a ActiveRecord::StaleObjectError</pre>

<p>You’re then responsible for dealing with the conflict by rescuing the
exception and either rolling back, merging, or otherwise apply the business
logic needed to resolve the conflict.</p>

<p>This locking mechanism will function inside a single Ruby process. To make
it work across all web requests, the recommended approach is to add
<code>lock_version</code> as a hidden field to your form.</p>

<p>This behavior can be turned off by setting
<code>ActiveRecord::Base.lock_optimistically = false</code>. To override
the name of the <code>lock_version</code> column, set the
<code>locking_column</code> class attribute:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">locking_column</span> = :<span class="ruby-identifier">lock_person</span>
<span class="ruby-keyword">end</span>
</pre>

    </div>
  


  


  
  


  
    <!-- Namespace -->
    <div class="sectiontitle">Namespace</div>
    <ul>
      
        <li>
          <span class="type">MODULE</span>
          <a href="Optimistic/ClassMethods.html">ActiveRecord::Locking::Optimistic::ClassMethods</a>
        </li>
      
    </ul>
  


  

  



  

    

    

    


    


    <!-- Methods -->
              </div>

    </div>
  </body>
</html>    